package com.whale.cloud.util.rest;

import com.whale.cloud.util.jdbc.JdbcAutoConfiguration;
import com.whale.cloud.util.jdbc.JdbcDataEngine;
import com.whale.cloud.util.rest.dataengine.MySQLDataEngine;
import com.whale.cloud.util.rest.dataengine.RedisDataEngine;
import com.whale.cloud.util.rest.query.*;
import com.whale.cloud.util.rest.schema.FunctionManager;
import com.whale.cloud.util.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/** Created by paco on 2017/06/08. */
@Configuration
@ComponentScan(
  basePackageClasses = {
    FunctionManager.class,
    RedisDataEngine.class,
    MySQLDataEngine.class,
    GetSysConfigure.class,
    JdbcDataEngine.class
  }
)
@ConditionalOnClass({
  Query.class,
  FunctionManager.class,
  RedisDataEngine.class,
  MySQLDataEngine.class,
  GetSysConfigure.class,
  JdbcDataEngine.class
})
@AutoConfigureAfter({JdbcAutoConfiguration.class})
//@Import({ConfigurableApplicationContext.class})
public class RestAutoConfiguration {
  @Autowired ConfigurableApplicationContext configurableApplicationContext;

  @Autowired private FunctionManager tableManager;

  @Bean(name = "query")
  @ConditionalOnMissingBean(Query.class)
  public Query getQuery() {
    Query query = new Query();
    //query.setDataDisposeEngine(de);
    SpringUtils.setApplicationContext(configurableApplicationContext);
    query.setFunctionManager(tableManager);
    return query;
  }

  @Bean(name = "create")
  @ConditionalOnMissingBean(ObjectCreate.class)
  public ObjectCreate create() {
    SpringUtils.setApplicationContext(configurableApplicationContext);
    ObjectCreate objectCreate = new ObjectCreate();
    return objectCreate;
  }

  @Bean(name = "update")
  @ConditionalOnMissingBean(ObjectModify.class)
  public ObjectModify update() {
    SpringUtils.setApplicationContext(configurableApplicationContext);
    ObjectModify om = new ObjectModify();
    return om;
  }

  @Bean(name = "delete")
  @ConditionalOnMissingBean(ObjectDelete.class)
  public ObjectDelete delete() {
    SpringUtils.setApplicationContext(configurableApplicationContext);
    ObjectDelete od = new ObjectDelete();
    return od;
  }
}
